﻿using Clothes.BLL;
using Clothes.Comment;
using Clothes.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YongHu_UI.Model;

namespace YongHu_UI
{
    public partial class login : System.Web.UI.Page
    {
        IAccountManager AccountManager = new AccountManager();
        CLLoginIBLL cLLogin = new CLLoginBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                var result = AccountManager.LoginByUserName(txtUserName.Text, txtPassword.Text);
                var deng = cLLogin.SelectSourceBLL(txtUserName.Text, txtPassword.Text);
                if (deng != null) {
                    int guan = deng.Manager;
                    //登录验证
                   CLUserBLL userManager = new CLUserBLL();
                    if (result.StatusCode == StatusCode.Succeed)
                    {
                        // 缓存用户资料
                        var user = result.ReadResult();
                        IdentityManager.SaveUser(deng);
                        // 脚本跳转
                        if (guan == 0)
                        {
                            Response.Redirect("index.aspx");
                        }
                        else
                        {
                            Response.Redirect("table.aspx");
                        }

                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(GetType(), "loginerror", $"alert('{result.Message}')", true);
                    }
                }
                ClientScript.RegisterStartupScript(GetType(), "loginerror", "alert('没有该用户')", true);
                
            }
        }
    }
}